﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;

public partial class SiteWorx_WebParts_Basic_Multimedia : BaseWebPart
{
    string _html;

    [Personalizable(PersonalizationScope.Shared)]
    public string HTML
    {
        get
        {
            return _html;
        }
        set
        {
            _html = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            litContent.Text = HTML;
            pnlEditContentButton.Visible = Page.User.IsInRole("Administrator") && !Utility.PageInPreviewMode();
        }
    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        RemoveWebPart();
    }

    protected override void RemoveWebPart()
    {
        base.RemoveWebPart();
    }

    protected void lnkbtnUpdateContent_Click(object sender, EventArgs e)
    {
        string contentId = hidContentId.Value;
        HTML = Session[contentId].ToString();
        litContent.Text = Session[contentId].ToString();

        //clear the content id and session variable
        hidContentId.Value = "";
        Session[contentId] = null;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string contentId = Guid.NewGuid().ToString() + "_Content";
        Session[contentId] = HTML;
        hidContentId.Value = contentId;

        string contentCssClass = "";
        if (ConfigurationManager.AppSettings["ContentEditorCssClass"] != null && ConfigurationManager.AppSettings["ContentEditorCssClass"].ToString().Length > 0)
            contentCssClass = ConfigurationManager.AppSettings["ContentEditorCssClass"].ToString();


        Telerik.Web.UI.RadWindow contentEditor = new Telerik.Web.UI.RadWindow();
        contentEditor.NavigateUrl = "~/ContentEditor.aspx?contentId=" + contentId + "&editorType=Multimedia&cssClass=" + contentCssClass;
        contentEditor.VisibleOnPageLoad = true;
        contentEditor.Modal = true;
        contentEditor.Behaviors = WindowBehaviors.Move | WindowBehaviors.Resize;
        contentEditor.ShowContentDuringLoad = false;
        contentEditor.VisibleStatusbar = false;

        if (ConfigurationManager.AppSettings["ContentEditorWindowWidth"] != null && ConfigurationManager.AppSettings["ContentEditorWindowWidth"].ToString().Length > 0)
            contentEditor.Width = Unit.Pixel(int.Parse(ConfigurationManager.AppSettings["ContentEditorWindowWidth"].ToString()));
        else
            contentEditor.Width = Unit.Pixel(1097);

        if (ConfigurationManager.AppSettings["ContentEditorWindowHeight"] != null && ConfigurationManager.AppSettings["ContentEditorWindowHeight"].ToString().Length > 0)
            contentEditor.Height = Unit.Pixel(int.Parse(ConfigurationManager.AppSettings["ContentEditorWindowHeight"].ToString()));
        else
            contentEditor.Height = Unit.Pixel(550);

        pnlEditContentButton.Controls.Add(contentEditor);

        BuildUpdateScript();
    }

    private void BuildUpdateScript()
    {
        string contentId = hidContentId.Value;
        if (contentId.Length > 0)
        {
            string functionName = "Update_" + contentId.Replace("-", "_");
            string script = "function " + functionName + "(sender){__doPostBack('" + lnkbtnUpdateContent.UniqueID + "','')}";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), functionName, script, true);
        }
    }
}